﻿Imports eCart.BusinessEntity.maestros
Imports System.Data.SqlClient
Namespace maestros
    Public Class daArticulos
        Function GetList(ByVal pModo As Integer,
                         ByVal pArticulo As beArticulos) As List(Of beArticulos)
            Dim lista As New List(Of beArticulos)
            Dim conexion As New SqlConnection
            Dim daData As New daDataConnection
            conexion = daData.Conectar
            Dim transaccion As SqlTransaction
            transaccion = conexion.BeginTransaction(IsolationLevel.ReadCommitted)
            Dim cmd As New SqlCommand
            Dim reader As SqlDataReader
            Try
                With cmd
                    .Connection = conexion
                    .CommandType = CommandType.StoredProcedure
                    .CommandText = "[maestros].[spArticulos]"
                    .Parameters.AddWithValue("@pModo", pModo)
                    .Parameters.AddWithValue("@pIdArticulo", pArticulo.IdArticulo)
                    .Parameters.AddWithValue("@pTitulo", pArticulo.Titulo)
                    .Parameters.AddWithValue("@pRowId", pArticulo.RowId)
                    .Transaction = transaccion
                    reader = .ExecuteReader
                End With

                While reader.Read
                    Dim oArticulo As New beArticulos
                    With oArticulo
                        .IdArticulo = IIf(reader.Item("IdArticulo") Is DBNull.Value, String.Empty, reader.Item("IdArticulo"))
                        .Titulo = IIf(reader.Item("Titulo") Is DBNull.Value, String.Empty, reader.Item("Titulo"))
                        .Detalle = IIf(reader.Item("Detalle") Is DBNull.Value, String.Empty, reader.Item("Detalle"))
                        .Isbn = IIf(reader.Item("Isbn") Is DBNull.Value, String.Empty, reader.Item("Isbn"))
                        .IdEditorial = IIf(reader.Item("IdEditorial") Is DBNull.Value, String.Empty, reader.Item("IdEditorial"))
                        .IdAutor = IIf(reader.Item("IdAutor") Is DBNull.Value, String.Empty, reader.Item("IdAutor"))
                        .AnioPublicacion = IIf(reader.Item("AnioPublicacion") Is DBNull.Value, String.Empty, reader.Item("AnioPublicacion"))
                        .Tamanio = IIf(reader.Item("Tamanio") Is DBNull.Value, String.Empty, reader.Item("Tamanio"))
                        .RowId = IIf(reader.Item("RowId") Is DBNull.Value, 0, reader.Item("RowId"))
                        .RowVersion = IIf(reader.Item("RowVersion") Is DBNull.Value, String.Empty, reader.Item("RowVersion"))
                    End With
                    lista.Add(oArticulo)
                End While
                transaccion.Commit()
            Catch ex As Exception
                transaccion.Rollback()
            Finally
                conexion.Close()
            End Try
            Return lista
        End Function

        Sub Write(ByVal pModo As Integer,
                  ByVal pArticulo As beArticulos)
            Dim conexion As New SqlConnection
            Dim daData As New daDataConnection
            conexion = daData.Conectar
            Dim cmd As New SqlCommand
            Dim transaccion As SqlTransaction
            transaccion = conexion.BeginTransaction(IsolationLevel.Serializable)
            Try
                With cmd
                    .Connection = conexion
                    .CommandType = CommandType.StoredProcedure
                    .CommandText = "[maestros].[spArticulos]"
                    .Parameters.AddWithValue("@pModo", pModo)
                    .Parameters.AddWithValue("@pIdArticulo", pArticulo.IdArticulo)
                    .Parameters.AddWithValue("@pTitulo", pArticulo.Titulo)
                    .Parameters.AddWithValue("@pDetalle", pArticulo.Detalle)
                    .Parameters.AddWithValue("@pIsbn", pArticulo.Isbn)
                    .Parameters.AddWithValue("@pIdEditorial", pArticulo.IdEditorial)
                    .Parameters.AddWithValue("@pIdAutor", pArticulo.IdAutor)
                    .Parameters.AddWithValue("@pAnioPublicacion", pArticulo.AnioPublicacion)
                    .Parameters.AddWithValue("@pTamanio", pArticulo.Tamanio)
                    .Parameters.AddWithValue("@pRowId", pArticulo.RowId)
                    .Parameters.AddWithValue("@pRowVersion", pArticulo.RowVersion)
                    .Transaction = transaccion
                    .ExecuteNonQuery()
                End With
                transaccion.Commit()
            Catch ex As Exception
                transaccion.Dispose()
            Finally
                conexion.Close()
            End Try
        End Sub

        Function isRegistrado(ByVal pModo As Integer,
                              ByVal pArticulo As beArticulos) As Boolean
            Dim blnResponse As Boolean
            Dim conexion As New SqlConnection
            Dim daData As New daDataConnection
            conexion = daData.Conectar
            Dim transaccion As SqlTransaction
            transaccion = conexion.BeginTransaction(IsolationLevel.ReadCommitted)
            Dim cmd As New SqlCommand
            Dim reader As SqlDataReader
            Try
                With cmd
                    .Connection = conexion
                    .CommandType = CommandType.StoredProcedure
                    .CommandText = "[maestros].[spArticulos]"
                    .Parameters.AddWithValue("@pModo", pModo)
                    .Parameters.AddWithValue("@pIdArticulo", pArticulo.IdArticulo)
                    .Transaction = transaccion
                    reader = .ExecuteReader
                End With

                reader.Read()
                If reader.HasRows Then
                    blnResponse = (pArticulo.IdArticulo = reader.Item("IdArticulo"))
                Else
                    blnResponse = False
                End If
                transaccion.Commit()
            Catch ex As Exception
                transaccion.Rollback()
            Finally
                conexion.Close()
            End Try
            Return blnResponse
        End Function
    End Class
End Namespace